package com.imay.live.view;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imay.live.R;

/**
 * Created by chan on 2017/5/21 0021.
 */

public class LoadView extends RelativeLayout {

    public static final int DEFAULT = 0;
    public static final int NONET = 1;
    public static final int NODATAS = 2;
    public static final int LOADING = 3;

    int status;

    ImageView progress;
    TextView load_text;
    TextView load_text2;

    public int getStatus() {
        return status;
    }

    public LoadView(Context context) {
        super(context);
        init(context);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.load_layout, this);
        progress = $(R.id.progress_icon, view);
        load_text = $(R.id.load_text, view);
        load_text2 = $(R.id.load_text2, view);
    }

    public void startLoading() {
        status = LOADING;
        progress.setVisibility(VISIBLE);
        progress.setImageResource(R.drawable.anim_loading_view);
        if (progress.getDrawable() instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) progress.getDrawable();
            if (!animationDrawable.isRunning()) {
                animationDrawable.start();
            }
            load_text.setVisibility(View.INVISIBLE);
            load_text2.setVisibility(View.INVISIBLE);
        }

    }

    public void stopLoading() {
        status = DEFAULT;
        progress.setVisibility(INVISIBLE);
        progress.setImageResource(R.drawable.anim_loading_view);
        if (progress.getDrawable() instanceof AnimationDrawable) {
            AnimationDrawable animationDrawable = (AnimationDrawable) progress.getDrawable();
            if (animationDrawable.isRunning()) {
                animationDrawable.stop();
            }
        }
        load_text.setVisibility(View.INVISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
    }

    public void loadError(String error) {
        load_text.setVisibility(View.VISIBLE);
        load_text.setText(error);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wushuju);
        progress.setVisibility(VISIBLE);
        load_text2.setVisibility(View.INVISIBLE);
    }

    public void loadError() {
        status = NODATAS;
        loadError(getContext().getString(R.string.str_nodata));
    }

    public void netError() {
        status = NONET;
        progress.setVisibility(VISIBLE);
        load_text.setVisibility(View.VISIBLE);
        load_text2.setVisibility(View.VISIBLE);
        progress.clearAnimation();
        progress.setImageResource(R.mipmap.wuwangluokeyong);
        load_text.setText(R.string.str_neterror);
        load_text2.setText(R.string.str_check_net);
    }

    private <T extends View> T $(int rid, View view) {
        return (T) view.findViewById(rid);
    }
}
